(function($, im, doc) {
	//长按消息框
	var msgTap = function(e) {
		//console.log('触发长按');
		var el = this;
		//console.log(this.getAttribute('msg-id'));
		if(el.querySelector('.msg-content-inner')) {
			var val = el.querySelector('.msg-content-inner').innerText;
		}else{
			var val = '';
		}
		
		
		var msg_type = 1;
		if(val == ''){
			var obj = el.innerHTML;
			if(el.querySelector('.msg-content-image')){
				val = el.querySelector('.msg-content-image').getAttribute('src');
			}else{
				val = '';
			}
			
			msg_type = 2;
		}
		var msgid = el.getAttribute('msg-id');
		
		plus.nativeUI.actionSheet({
			title: "操作",
			cancel: "取消",
			buttons: [{
				title: "复制"
			}, {
				title: "收藏"
			}, {
				title: "删除"
			}/*, {
				title: "发送给朋友"
			}, {
				title: "撤回"
			}, {
				title: "删除"
			}*/]
		}, function(e) {
			console.log("User pressed: " + e.index);
			
			if(e.index == 1) {
				
				if(mui.os.android) {
					 var Context = plus.android.importClass("android.content.Context");
				    var main = plus.android.runtimeMainActivity();
				    var clip = main.getSystemService(Context.CLIPBOARD_SERVICE);
				    plus.android.invoke(clip,"setText",val);
				} else {
					var UIPasteboard = plus.ios.importClass("UIPasteboard");
					var generalPasteboard = UIPasteboard.generalPasteboard();
					generalPasteboard.setValueforPasteboardType(val, "public.utf8-plain-text");
				}
				$.toast("已复制");
			}else if(e.index == 2) {
				var toUrl = apiurl + 'Favorite/addCollect';

                mui.post(toUrl, {
                	uid: app.getState().user_id,
					uname: app.getState().name,
                    c_id : im.roomId,
                    c_name : im.chatName,
                    c_msg : val,
                    c_type : 2,
                    msg_type : msg_type
				}, function(res){
                    if(res.status == 1) {
                        // mui.alert('已收藏!');
                        $.toast('已收藏');
                    }
                })
			} else if(e.index == 3) {
				
				im.chat.delOne(im.roomId, msgid);
				el.remove();
			}  else if(e.index == 4) {
				$.toast("开发中");
			}   else if(e.index == 5) {
				$.toast("开发中");
			} 
			return true;
		});
	};
	//点击顶部用户头像
	var navbarTap = function() {
		if(im.roomType == 1) {
			mui.openWindow({
				url: 'chat_info.html',
				id: 'chat_info.html',

				show: {
					autoShow: true,
					aniShow: 'slide-in-right',
					duration: 100
				},
				waiting: {
					autoShow: true,
					title: 'waiting...'
				},
				extras: {
					chatHeader: im.header,
					chatName: im.chatName,
					roomId: im.roomId
				}
			});
		} else {
			mui.openWindow({
				url: 'group_set.html',
				id: 'group_set.html',

				show: {
					autoShow: true,
					aniShow: 'slide-in-right',
					duration: 100
				},
				waiting: {
					autoShow: true,
					title: 'waiting...'
				},
				extras: {
					chatHeader: im.header,
					chatName: im.chatName,
					roomId: im.roomId
				}
			});
		}

	};
	//查看用户资料
	var showUserMgr = function() {
		var fid = this.getAttribute('fid');
		mui.openWindow({
			url: '/pages/user/other_detail.html',
			id: '/pages/user/other_detail.html',

			show: {
				autoShow: true,
				aniShow: 'slide-in-right',
				duration: 100
			},
			waiting: {
				autoShow: true,
				title: 'waiting...'
			},
			extras: {
				fid: fid
			}
		})
	};
	$.plusReady(function() {
		//长按消息框
		$('#msg-list').on('longtap', '.msg-item', msgTap);
		//点击顶部用户头像
		//$('#rightuser').on('tap', navbarTap);
		document.getElementById('rightuser').addEventListener('tap', navbarTap);
		//查看用户资料
		mui('#msg-list').on('tap', '.msg-user-img', showUserMgr);
	});
}(mui, im, document));